<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2020/10/12
 * Time: 4:06 PM
 */
namespace app\admin\logic;

use app\model\User;
use think\facade\Db;
use think\facade\Request;

class UserLogic
{

    public function getList()
    {
        $param = Request::get();

        $where = [];

        if(isset($param['pername']) || !empty($param['pername'])){
            $where[] = ['pername','like','%'.trim($param['pername']).'%'];
        }
        if(isset($param['phone']) || !empty($param['phone'])){
            $where[] = ['phone','like','%'.trim($param['phone']).'%'];
        }

        if(isset($param['position']) || !empty($param['position'])){
            $where[] = ['position','=',intval($param['position'])];
        }

        $where[] = ['status','=',1];    //默认是 没删除的
        $where[] = ['orgid','=',config('user')['orgid']];
        $userModel = new User();
        $list = $userModel->getUserListByCondition($where);
        return $list;
    }

    public function addUser()
    {
        $param = Request::post();
        $user = [];
        if(!isset($param['pername']) || empty($param['pername']))   fail('姓名不能为空');
        $user['pername'] = trim($param['pername']);

        if(!isset($param['phone']) || empty($param['phone']))   fail('手机号码不能为空');
        $user['phone'] = trim($param['phone']);

        if(!isset($param['contact']) || empty($param['contact']) || !checkNormalPhone($param['contact']))   fail('联系方式不能为空或者格式错误');
        $user['contact'] = trim($param['contact']);

        if(!isset($param['position']))   fail('请选择职位');
        $user['position'] = strval($param['position']);

        $user['orgid'] = config('user')['orgid'];

        $userModel = new User();
        $info = $userModel->getUserInfoByPhone($user['phone']);
        if($info){
            //存在手机号
            if($info['status'] == 0){
                //已经删除的 更新
                $user['updateid'] = config('user')['id'];
                $user['status'] = 1;
                $update = $userModel->updateUserInfoByWhere(['id'=>$info['id']],$user);
                if(!$update)    fail('人员添加失败');
            }else{
                fail('手机号用户已存在');
            }
        }

        Db::startTrans();
        $user['createid'] = config('user')['id'];
        $user['createname'] = config('user')['pername'];

        try {
            $userId = $userModel->addOneUser($user);
            if(!$userId){
                Db::rollback();
                fail('人员添加失败');
            }
            Db::commit();
        }
        catch (\Exception $e) {
            Db::rollback();
            fail($e);
        }

        return true;

    }
    public function updateUser()
    {
        $param = Request::post();
        $userModel = new User();
        if(!isset($param['id']) || empty($param['id']))   fail('请选择人员');
        $id = intval($param['id']);
        $info = $userModel->getUserInfoById($id);

        if(!$info)  fail('人员信息不存在');

        $user = [
            'updateid' => config('user')['id'],
            'updatename' => config('user')['pername']
        ];
        if(!isset($param['pername']) || empty($param['pername']))   fail('姓名不能为空');
        $user['pername'] = trim($param['pername']);

        if(!isset($param['phone']) || empty($param['phone']))   fail('手机号码不能为空');
        $user['phone'] = trim($param['phone']);

        if(!isset($param['contact']) || empty($param['contact']) || !checkNormalPhone($param['contact']))   fail('联系方式不能为空或者格式错误');
        $user['contact'] = trim($param['contact']);

        if($user['phone'] != $info['phone']){
            $newInfo = $userModel->getUserInfoByPhone($user['phone']);
            if($newInfo)   fail('修改后的手机号用户已存在，请更换手机号码');
        }

        if(!isset($param['position']))   fail('请选择职位');
        $user['position'] = strval($param['position']);

        Db::startTrans();
        try {
            $userId = $userModel->updateUserInfoByWhere(['id'=>$id],$user);
            if(!$userId){
                Db::rollback();
                fail('人员信息修改失败');
            }
            Db::commit();
        }
        catch (\Exception $e) {
            Db::rollback();
            fail($e);
        }

        return true;

    }
    public function del($id)
    {
        $userModel = new User();
        $userInfo = $userModel->getUserInfoById($id);

        if(!$userInfo)   fail('人员信息不存在');

        if($userInfo['status'] == 0)    fail('人员已经删除，不能重复操作');

        $user = [
            'status' => 0,
            'updateid' => config('user')['id'],
            'updatename' => config('user')['pername'],
        ];

        $rt = $userModel->updateUserInfoByWhere(['id'=>$id],$user);
        if(!$rt)    fail('操作失败，请稍后重试');

        return true;
    }

}